
package org.masterinftel.scrumpmt.adapters;

import java.util.ArrayList;
import java.util.List;

import org.masterinftel.scrumpmt.Constants;
import org.masterinftel.scrumpmt.ScrollState;
import org.masterinftel.scrumpmt.ScrumApplication;
import org.masterinftel.scrumpmt.json.model.EsfuerzoJSON;
import org.masterinftel.scrumpmt.json.model.TareaJSON;
import org.masterinftel.scrumpmt.json.model.Task;

import android.app.Activity;
import android.content.Context;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.jakewharton.android.viewpagerindicator.TitleProvider;

public class TasksViewPagerAdapter extends PagerAdapter implements TitleProvider {
    private static String[] titles = new String[] {
            Constants.PAGE_TITLE_TODO,
            Constants.PAGE_TITLE_DOING,
            Constants.PAGE_TITLE_DONE
    };
    private final Context context;
    private ScrumApplication application;
    private int[] scrollPosition = new int[titles.length];
    private List<TareaJSON> tareas;

    private ArrayAdapter<Task> mListTODO;
    private ArrayAdapter<Task> mListDOING;
    private ArrayAdapter<Task> mListDONE;

    public TasksViewPagerAdapter(Context context) {
        this.context = context;
        for (int i = 0; i < titles.length; i++) {
            scrollPosition[i] = 0;
        }
        
        application = (ScrumApplication) context.getApplicationContext();
        tareas = application.getTareas();

        // Carga inicial de listas
        mListTODO = new TaskArrayAdapter((Activity) context, getTask(Constants.PAGE_TODO));
        mListDOING = new TaskArrayAdapter((Activity) context, getTask(Constants.PAGE_DOING));
        mListDONE = new TaskArrayAdapter((Activity) context, getTask(Constants.PAGE_DONE));
    }


    public ArrayAdapter<Task> getmListTODO() {
        return mListTODO;
    }

    public ArrayAdapter<Task> getmListDOING() {
        return mListDOING;
    }

    public ArrayAdapter<Task> getmListDONE() {
        return mListDONE;
    }

    public void setmListTODO(ArrayAdapter<Task> mListTODO) {
        this.mListTODO = mListTODO;
    }


    public void setmListDOING(ArrayAdapter<Task> mListDOING) {
        this.mListDOING = mListDOING;
    }


    public void setmListDONE(ArrayAdapter<Task> mListDONE) {
        this.mListDONE = mListDONE;
    }


    @Override
    public String getTitle(int position) {
        return titles[position];
    }
    
    @Override
    public int getCount() {
        return titles.length;
    }

    @Override
    public Object instantiateItem(View pager, final int position) {

        ListView v = new ListView(context);
        
        switch (position) {
            case Constants.PAGE_TODO:
                v.setAdapter(mListTODO);
                break;
            case Constants.PAGE_DOING:
                v.setAdapter(mListDOING);
                break;
            case Constants.PAGE_DONE:
                v.setAdapter(mListDONE);
                break;
        }

        ((ViewPager) pager).addView(v, 0);
        v.setSelection(scrollPosition[position]);
        v.setOnScrollListener(new OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem,
                    int visibleItemCount, int totalItemCount) {
                scrollPosition[position] = firstVisibleItem;
            }
        });

        return v;
    }

    @Override
    public void destroyItem(View pager, int position, Object view) {
        ((ViewPager) pager).removeView((ListView) view);
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view.equals(object);
    }

    @Override
    public void finishUpdate(View view) {
    }

    @Override
    public void restoreState(Parcelable p, ClassLoader c) {
        if (p instanceof ScrollState) {
            scrollPosition = ((ScrollState) p).getScrollPos();
        }
    }

    @Override
    public Parcelable saveState() {
        return new ScrollState(scrollPosition);
    }

    @Override
    public void startUpdate(View view) {
    }

    public List<Task> getTask(int column) {
        List<Task> tasks = new ArrayList<Task>();

        for (TareaJSON t : tareas) {
            int estado = (int) t.getEstado();
            estado--;
            if (column == estado) {
                tasks.add(get(t.getId(), t.getDes(), t.getColabName(), t.getEsfuerzos()));
            }
        }
        
        return tasks;
    }

    private Task get(long id, String d, String m, List<EsfuerzoJSON> le) {
        return new Task(id, d, m, le);
    }


}
